import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/config/app_config.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/config/history_route_ovserver.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_commission/deposit_record_commission_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_recharge/deposit_record_recharge_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_commission/deposit_record_team_commission_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_list/deposit_record_team_list_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_withdraw/deposit_record_withdraw_logic.dart';
import 'package:flutter_09_game/pages/home/home_logic.dart';
import 'package:flutter_09_game/pages/main/keep_alive_widget.dart';
import 'package:flutter_09_game/pages/main/model_app_package.dart';
import 'package:flutter_09_game/pages/profile/profile_logic.dart';
import 'package:flutter_09_game/pages/promotion/promotion_commission/promotion_commission_logic.dart';
import 'package:flutter_09_game/pages/promotion/promotion_logic.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/dialog/notice_popup_dialog.dart';
import 'package:flutter_09_game/widgets/dialog/widget_dialog_package.dart';
import 'package:get/get.dart';

import '../home/home_view.dart';
import '../promotion/promotion_view.dart';
import '../deposit/deposit_view.dart';
import '../welfare/welfare_view.dart';
import '../profile/profile_view.dart';

class MainLogic extends GetxController {
  // 当前选中的页面索引
  final RxInt currentIndex = 0.obs;

  int lastPopTime = 0;

  final PageController pageController = PageController(initialPage: 0);
  final List<Widget> pages = [
    KeepAliveWrapper(child: HomePage()),
    PromotionPage(),
    KeepAliveWrapper(child: DepositPage()),
    KeepAliveWrapper(child: WelfarePage()),
    KeepAliveWrapper(child: ProfilePage()),
  ];

  @override
  void onInit() {
    super.onInit();

    getSettingConfig();
    // getAppPackage();
// UtilsOther().getAppPackage();
    // if(AppUserManager().isLogin()) {
    //   AppUserManager().refreshUserInfo();
    //   AppUserManager().loadWalletData();
    // }
  }

  @override
  void onReady() {
    super.onReady();

    // 页面准备就绪时的操作
    // 确保每次进入主页都显示首页
    // resetToHome();
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }

  /// 切换页面索引
  void changeIndex(int index) {
    if (currentIndex.value == index) return;
    if (AppUserManager().isLogin() == false) {
      Get.toNamed(AppRoutes.loginPage);
      return;
    }
    currentIndex.value = index;
    if (index == 4) {
      AppUserManager().refreshUserInfo();
      AppUserManager().loadWalletData();
      if (Get.isRegistered<ProfileLogic>()) {
        Get.find<ProfileLogic>().beginWalletAnimated();
      }
    } else if (index == 0) {
      Get.find<HomeLogic>().loadNoticeData();
      Get.find<HomeLogic>().beginPoolAnimated();
    } else if (index == 1) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (Get.isRegistered<PromotionLogic>()) {
          if (Get.find<PromotionLogic>().currentTabIndex.value == 1 &&
              Get.isRegistered<DepositRecordTeamLogic>(tag: '2')) {
            int index = Get.find<DepositRecordTeamLogic>(tag: '2')
                .currentIndex;
            List<String> options = UtilsOther.recordTeamOptions;
            if (index == 3) {
              requestRefresh<DepositRecordTeamCommissionLogic>(
                  tag: '2');
            } else {
              requestRefresh<DepositRecordTeamListLogic>(
                  tag: '${options[index]}2');
            }
          } else if (Get.find<PromotionLogic>().currentTabIndex.value == 0 &&
              Get.isRegistered<PromotionCommissionLogic>()) {
            // PromotionCommissionPage
            requestRefresh<PromotionCommissionLogic>();
          }
        }
      });
    }
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (pageController.hasClients) {
        pageController.jumpToPage(index);
      }
    });
  }

  requestRefresh<T extends BaseListMoreController>({String? tag}) {
    if (Get.isRegistered<T>(tag: tag)) {
      try {
        clog('adljasdlkjs $tag');
        Get.find<T>(tag: tag).refreshController.requestRefresh();
      } catch (_) {
        clog('adljasdlkjscatch $tag');
      }
    }
  }

  getSettingConfig() async {
    HttpResponse response = await HttpManager().get(
      API.getSetting,
      // fromJson: .fromJson,
    );
    if (response.isSuccess) {
      AppConfig.settingConfigs = response.data;
      // return response.data;
    }
  }
}
